// pages/trade/trade.js
const app = getApp()
import Http from '../../utils/http.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageSize: 10,
    pageNumber: 1,
    allLoaded: false,
    list: [],
    queryString: '',
    inputVal: '',


    startX: 0,
    buttonWidth: 160,
    winH: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //获取屏幕高度用于触发上拉加载
    //console.log('onload')
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          winH: res.windowHeight
        });
      }
    });
    //console.log(this.data.winH)
    this.loadData();
  },

  onShow: function(options) {


  },
  //滚动到底部触发事件  
  searchScrollLower: function() {
    // console.log('loading----------')
    // console.log(this.data.allLoaded)
    // console.log(this.data.pageNumber)
    if (!this.data.allLoaded) {
      this.setData({
        pageNumber: this.data.pageNumber + 1,
      });
      this.loadData();
    } else {
      wx.showToast({
        title: '已全部加载',
        icon: 'success',
        duration: 1000
      });
    }
  },
  //加载数据
  loadData: function() {
    Http.request({
      url: '/wxApplication/findUserApplicationPage',
      data: {
        queryString: this.data.queryString,
        pageSise: this.data.pageSize,
        pageNumber: this.data.pageNumber
      },
      success: (res) => {
        // console.log(res)
        var data = this.data.list;
        if (res.totalElements > 0) {
          data = data.concat(res["content"]);
        }
        this.setData({
          list: data,
          allLoaded: res["totalPages"] <= this.data.pageNumber
        });
      }
    });
  },
  //完全重新加载数据
  reloadData: function() {
    this.setData({
      list: [],
      allLoaded: false,
      pageSize: 10,
      pageNumber: 1,
    });
    this.loadData();
  },
  //下拉刷新
  freshData: function() {
    //console.log('下拉刷新')
    this.reloadData()

  },
  //修改按钮
  updateApp: function(e) {
    var index = e.currentTarget.dataset.index;

    var status = e.currentTarget.dataset.status;
    //console.log(status)
    //只有未开票的允许修改
    if (status == 0 ) {
      wx.navigateTo({
        url: 'apply/apply?applicationId=' + index
      })


    } else {
      wx.navigateTo({
        url: '/pages/finished/detail/detail?applicationId=' + index
      })

    }

  },
  //提交
  submitApp: function(e) {

  },
  //删除
  // deleteApp: function(e) {
  //   let that = this;
  //   wx.showModal({
  //     content: '是否确定删除该申请单？',
  //     showCancel: true,
  //     success: function(res) {
  //       if (res.confirm) {
  //         var index = e.currentTarget.dataset.index;
  //         var list = that.data.list;
  //         var updateData = list[index];
  //         //删除申请单
  //         Http.request({
  //           url: '/application/deleteApplication',
  //           data: {
  //             applicationId: updateData.applicationId
  //           },
  //           success: (res) => {
  //             that.reloadData();
  //           }
  //         });

  //       }
  //     }
  //   });
  // },
  //左滑效果
  touchS: function(e) {
    if (e.touches.length == 1) {
      this.setData({
        //设置触摸起始点水平方向位置
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function(e) {
    //手指移动时水平方向位置
    var moveX = e.touches[0].clientX;
    //手指起始点位置与移动期间的差值
    var disX = this.data.startX - moveX;
    var buttonWidth = this.data.buttonWidth;
    var right = 0;
    if (disX == 0 || disX < 0) { //如果移动距离小于等于0，文本层位置不变
      right = 0;
    } else if (disX > 0) { //移动距离大于0，文本层left值等于手指移动距离
      right = disX;
      if (disX >= buttonWidth) {
        //控制手指移动距离最大值为删除按钮的宽度
        right = buttonWidth;
      }
    }
    //获取手指触摸的是哪一项
    var index = e.currentTarget.dataset.index;
    var list = this.data.list;
    list[index].right = right;
    //更新列表的状态
    this.setData({
      list: list
    });
  },
  touchE: function(e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = this.data.startX - endX;
      var buttonWidth = this.data.buttonWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var right = disX > buttonWidth / 2 ? buttonWidth : 0;
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      var list = this.data.list;
      list[index].right = right;
      // //更新列表的状态
      this.setData({
        list: list
      });
    }
  },

  clearInput: function() {
    this.setData({
      inputVal: ""
    });
  },
  clickSearch: function() {
    //console.log(this.data.inputVal)
    this.setData({
      queryString: this.data.inputVal
    })
    this.reloadData();
  },
  inputTyping: function(e) {
    this.setData({
      inputVal: e.detail.value
    });
  }
})